﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**
 * 创建时间：2014/10/29 15:58:57
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace Assistant
{
    public class EncodeHelper
    {
        public static string DecodeBase64(string base64Json)
        {
            if (base64Json == null)
            {
                return null;
            }
            string str = null;
            Encoding encoding = Encoding.UTF8;
            try
            {
                byte[] bytes = Convert.FromBase64String(base64Json);
                str = encoding.GetString(bytes, 0, bytes.Length);
            }
            catch (Exception err)
            {
                LogHelper.LogInfo(err.ToString(), typeof(EncodeHelper));
                return null;
            }
            if (str == null)
            {
                return null;
            }
            return str;
        }

        public static string EncodeBase64(string code_type, string code)
        {
            if ((code_type == null) || (code == null))
            {
                return "";
            }
            try
            {
                byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
                return Convert.ToBase64String(bytes);
            }
            catch (Exception)
            {
                return code;
            }
        }

        public static string EncodeBase64(byte[] code)
        {
            if (code == null)
                return string.Empty;
            try
            {
                return Convert.ToBase64String(code);
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
